Public Class Form1
    'Get the game ready to start again
    Sub ResetGame()
        Dim ctrl As Control
        'Loop through the board controls and set them to ""
        For Each ctrl In Me.Controls
            If TypeOf (ctrl) Is Button And ctrl.Name <> "btnNewGame" Then
                ctrl.Text = String.Empty
            End If
        Next
        lblMessages.Text = ""
        'Enable the board buttons
        CorrectEnabledState(True)
    End Sub

    Private Sub CorrectEnabledState(ByVal ButtonEnabledState As Boolean)
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf (ctrl) Is Button And ctrl.Name <> "btnNewGame" Then
                ctrl.Enabled = ButtonEnabledState
            End If
        Next
    End Sub

    Private Sub CorrectEnabledState()
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf (ctrl) Is Button And ctrl.Name <> "btnNewGame" Then
                If ctrl.Text = String.Empty Then
                    ctrl.Enabled = True
                Else
                    ctrl.Enabled = False
                End If
            End If
        Next
    End Sub

    Sub ComputerPlay()
        Dim RandomGenerator As New Random()
        Dim intRandom As Integer
        Dim intCount As Integer = 0
        Dim ctrl As Control
        intRandom = RandomGenerator.Next(20, 100)
        While intCount < intRandom
            For Each ctrl In Me.Controls
                If TypeOf (ctrl) Is Button And ctrl.Name <> "btnNewGame" Then
                    If ctrl.Text = String.Empty Then
                        intCount += 1
                        If intCount = intRandom Then
                            ctrl.Text = "O"
                            ctrl.Enabled = False
                            Exit For
                        End If
                    End If
                End If
            Next
        End While
    End Sub

    Private Sub TicTacToe_Click(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles btn00.Click, btn20.Click, btn10.Click, _
    btn01.Click, btn21.Click, btn11.Click, btn02.Click, btn22.Click, btn12.Click
        CorrectEnabledState(False)
        Application.DoEvents() 'Allows the screen to refresh
        CType(sender, Button).Text = "X"
        If IsGameOver() Then
            MsgBox("Game Over")
        Else
            lblMessages.Text = "Computer selecting ..."
            Application.DoEvents() 'Allows the screen to refresh
            ComputerPlay()
            If IsGameOver() Then
                MsgBox("Game Over")
            Else
                lblMessages.Text = "Select your next position ..."
                CorrectEnabledState()
            End If
        End If
    End Sub

    Sub Winner(ByVal strWinner As String)
        Dim strMessage As String
        If strWinner = "X" Then
            strMessage = "You win!!"
        ElseIf strWinner = "O" Then
            strMessage = "Computer wins!!"
        Else
            strMessage = strWinner
        End If
        lblMessages.Text = strMessage
    End Sub

    Function IsGameOver() As Boolean
        If btn00.Text = btn01.Text And btn01.Text = btn02.Text And _
        btn02.Text <> String.Empty Then
            'Winner on top Row
            Call Winner(btn00.Text)
            Return True
        End If

        If btn10.Text = btn11.Text And btn11.Text = btn12.Text And _
        btn12.Text <> String.Empty Then
            'Winner on middle Row
            Call Winner(btn10.Text)
            Return True
        End If

        If btn20.Text = btn21.Text And btn21.Text = btn22.Text And _
        btn22.Text <> String.Empty Then
            'Winner on bottom Row
            Call Winner(btn20.Text)
            Return True
        End If

        If btn00.Text = btn10.Text And btn10.Text = btn20.Text And _
        btn20.Text <> String.Empty Then
            'Winner on first column
            Call Winner(btn00.Text)
            Return True
        End If

        If btn01.Text = btn11.Text And btn11.Text = btn21.Text And _
        btn21.Text <> String.Empty Then
            'Winner on second column
            Call Winner(btn01.Text)
            Return True
        End If

        If btn02.Text = btn12.Text And btn12.Text = btn22.Text And _
        btn22.Text <> String.Empty Then
            'Winner on third column
            Call Winner(btn02.Text)
            Return True
        End If

        If btn00.Text = btn11.Text And btn11.Text = btn22.Text And _
        btn22.Text <> String.Empty Then
            'Winner on diagonal top left to bottom right
            Call Winner(btn00.Text)
            Return True
        End If

        If btn20.Text = btn11.Text And btn11.Text = btn02.Text And _
        btn02.Text <> String.Empty Then
            'Winner on diagonal bottom left to top right
            Call Winner(btn20.Text)
            Return True
        End If

        'Test for a tie, all square full
        Dim ctrl As Control
        Dim intOpenings As Integer = 0
        For Each ctrl In Me.Controls
            If TypeOf (ctrl) Is Button And ctrl.Name <> "btnNewGame" Then
                If ctrl.Text = String.Empty Then
                    intOpenings = intOpenings + 1
                End If
            End If
        Next
        If intOpenings = 0 Then
            Call Winner("It's a tie.")
            Return True
        End If
        Return False
    End Function

    Private Sub TicTacToe_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CorrectEnabledState(False)
        lblMessages.Text = "Click new game to begin."

    End Sub

    Private Sub btnNewGame_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
        ResetGame()
    End Sub
End Class
